# 作者：陈睿-cruise
# 2024年03月13日22时08分01秒
# 2979386061@qq.com
# 爬取结构化数据，以百度电影热搜为例
from selenium import webdriver
from selenium.webdriver.common.by import By

list_movie=[]
with webdriver.Chrome() as driver:
    url='https://top.baidu.com/board?tab=movie'
    driver.get(url)
    # 每行//div[@class="category-wrap_iQLoo"]
    # 电影名称（列）//div[@class="c-single-text-ellipsis"]
    # 类型（列）//div[@class="content_1YWBm"]/div[@class="intro_1l0wp"][1]
    # 演员（列）//div[@class="content_1YWBm"]/div[@class="intro_1l0wp"][2]
    # 电影简介（列）//div[@class="c-single-text-ellipsis desc_3CTjT"]
    for item in driver.find_elements(By.XPATH,'//div[@class="category-wrap_iQLoo"]'):
        dict_movie={
            # xpath需要以.开头（相对路径）
            "片名":item.find_element(By.XPATH, './/div[@class="c-single-text-ellipsis"]').text,
            "类型":item.find_element(By.XPATH, './/div[@class="content_1YWBm"]/div[@class="intro_1l0wp"][1]').text,
            "演员":item.find_element(By.XPATH, './/div[@class="content_1YWBm"]/div[@class="intro_1l0wp"][2]').text,
            "简介": item.find_element(By.XPATH, './/div[@class="c-single-text-ellipsis desc_3CTjT"]').text,
        }
        list_movie.append(dict_movie)
print(list_movie)
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
#
# list_movie = []
#
# # 创建 WebDriver 实例
# with webdriver.Chrome() as driver:
#     url = 'https://top.baidu.com/board?tab=movie'
#     driver.get(url)
#
#     # 创建 WebDriverWait 实例，设置最大等待时间为10秒
#     wait = WebDriverWait(driver, 10)
#
#     try:
#         # 打印页面标题
#         print("页面标题:", driver.title)
#
#         # 等待直到至少一个电影元素加载完成
#         print("等待页面加载...")
#         wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="c-single-text-ellipsis"]')))
#         print("页面加载完成")
#
#         # 获取电影元素列表
#         items = driver.find_elements(By.XPATH, '//div[@class="category-wrap_iQLoo"]')
#
#         print("找到 {} 个电影元素".format(len(items)))  # 打印找到的电影元素数量
#
#         for item in items:
#             # 打印整个item元素的HTML代码
#             print("Item HTML:", item.get_attribute("outerHTML"))
#             dict_movie = {
#                 "片名": item.find_element(By.XPATH, './/div[@class="c-single-text-ellipsis"]').text,
#                 "类型": item.find_element(By.XPATH, './/div[@class="content_1YWBm"]/div[@class="intro_1l0wp"][1]').text,
#                 "演员": item.find_element(By.XPATH, './/div[@class="intro_1l0wp"][contains(text(), "演员")]').text,
#                 "简介": item.find_element(By.XPATH, './/div[@class="c-single-text-ellipsis desc_3CTjT"]').text
#             }
#             list_movie.append(dict_movie)
#
#     except Exception as e:
#         print("发生异常:", e)
#
# print(list_movie)
